package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcde.luowowo.website.annotation.LoginRequired;
import cn.wolfcde.luowowo.website.annotation.RedisParameter;
import cn.wolfcde.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.comment.domain.ItemShopComment;
import cn.wolfcode.luowowo.comment.query.ItemShopCommentQueryObject;
import cn.wolfcode.luowowo.comment.service.IItemShopCommentService;
import cn.wolfcode.luowowo.common.customexception.UserInfoException;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import cn.wolfcode.luowowo.itemshop.domain.ItemShopProp;
import cn.wolfcode.luowowo.itemshop.qo.ShopPropQueryObject;
import cn.wolfcode.luowowo.itemshop.service.IItemShopPropService;
import cn.wolfcode.luowowo.itemshop.service.IUserPropService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
public class ItemShopController {

    //商店道具列表
    private static final int ITEMSHOP_PROP = 0;

    //用户道具列表
    private static final int USER_PROP = 1;

    @Reference
    private IItemShopPropService itemShopPropService;

    @Reference
    private IUserPropService userPropService;

    @Reference
    private IItemShopCommentService iItemShopCommentService;


    /**
     * 商城主页
     *
     * @param userInfo
     * @return
     */
    @RequestMapping("mall/mall")
    public String index(@RedisParameter UserInfo userInfo) {

        return "itemshop/list";
    }

    /**
     * 请求道具数据
     *
     * @param userInfo
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("itemshop/query")
    public String itemshop(@RedisParameter UserInfo userInfo, @ModelAttribute("qo") ShopPropQueryObject qo, Model model) {

        if (qo.getQueryType() == ITEMSHOP_PROP) {

            PageInfo pageInfo = itemShopPropService.query(qo);

            model.addAttribute("pageInfo", pageInfo);

            return "itemshop/itemTempl";

        } else if (qo.getQueryType() == USER_PROP) {

            if (userInfo != null) {

                qo.setUserId(userInfo.getId());

                PageInfo pageInfo = userPropService.queryByUserId(qo);

                model.addAttribute("pageInfo", pageInfo);

                return "itemshop/myItemTempl";
            }

        } else {
            return "itemshop/myItemTempl";
        }


        //这里逻辑有错,要做未登录判断
        return null;
    }

    /**
     * 评论添加并回显
     *
     * @param userInfo
     * @param comment
     * @return
     */
    @RequestMapping("itemshop/comment")
    public String itemshop(@RedisParameter UserInfo userInfo, ItemShopComment comment, Model model, @ModelAttribute("qo") ItemShopCommentQueryObject qo) {

        if (userInfo != null) {

            comment.setFromId(userInfo.getId());
            comment.setFromName(userInfo.getNickname());
            comment.setFromLevel(userInfo.getLevel());
            comment.setFromImg(userInfo.getHeadImgUrl());
            comment.setDate(new Date());

        }

        if (StringUtils.hasLength(comment.getContent())) {

            iItemShopCommentService.addComment(comment);
        }

        Page page = iItemShopCommentService.queryPageForComment(qo);

        model.addAttribute("page", page);

        return "itemshop/commentTempl";
    }

    /**
     * 购买方法
     *
     * @param userInfo
     * @param id       商品ID
     * @return
     */
    @RequestMapping("/itemshop/buy")
    @ResponseBody
    public AjaxResult itemshop(@RedisParameter UserInfo userInfo, Long id, HttpServletRequest request, HttpServletResponse response) {

        AjaxResult ajaxResult = new AjaxResult();

        if (userInfo == null) {

            ajaxResult.mark("请登录后购买");

            ajaxResult.setCode(403);    //跳转登录

            return ajaxResult;
        }

        //先获取道具
        ItemShopProp itemShopProp = itemShopPropService.getById(id);

        if (itemShopProp == null || itemShopProp.getRest() <= 0) {

            throw new UserInfoException("暂无此商品或者没有库存");

        }
        String token = CookieUtil.getToken(request, response);
        //购买此道具
        userPropService.buy(userInfo, itemShopProp, token);

        return ajaxResult;
    }


}
